home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6387 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.6 KB  |  69 lines

  1. Path: news.clark.net!usenet
  2. From: yom@clark.net (yom)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: changing strings via pointers
  5. Date: 24 Feb 1996 02:11:53 GMT
  6. Organization: Your Organization
  7. Message-ID: <4gls59$rct@clarknet.clark.net>
  8. References: <1996Feb22.125436.25503@leeds.ac.uk>
  9. NNTP-Posting-Host: yom.clark.net
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. X-Newsreader: WinVN 0.99.7
  13.  
  14. You can use strchr string function to get the pointer to a newline
  15. character in your character array, and set what the pointer points
  16. to to NULL (0).
  17.  
  18.     char *b;
  19.     if (b = strchr(buffer,'\n'))
  20.         *b = 0;
  21.  
  22. Song (yom@clark.net)
  23.  
  24. In article <1996Feb22.125436.25503@leeds.ac.uk>, csyamc@scs.leeds.ac.uk 
  25. says...
  26. >
  27. >I reading in strings from a file using fgets, which stores the "\n" in
  28. >the array of chars which I have declared using a pointer.
  29. >
  30. >The thing is, I want to get rid of the "\n" and replace it with "\0", 
  31. but
  32. >obviously I can only do this using the pointer. I'm using the 
  33. following code:
  34. >
  35. >char *Contents < pointer to string
  36. >
  37. >int count = 0;
  38. >
  39. >
  40. >while (*(Contents + count)!= NULL)  /* the end of string is not 
  41. reached */
  42. >{if (*(Contents + count) == atoi("\n"))
  43. >/* change the \n to a \0 and set the next char along to null */
  44. >{
  45. >*(Contents + count) = atoi("\0");
  46. >
  47. >*(Contents + count + 1) = NULL;
  48. >
  49. >
  50. >} 
  51. >else
  52. >/* read in the next char */
  53. >{
  54. >count++;
  55. >
  56. >I think the problem maybe my way of using the pointer - I can print 
  57. out each
  58. >char as I get to it, but it doesnt seem to change the array at all.
  59. >Should I be using atoi?, it doesnt seem to work without it.
  60. >
  61. >Cheers
  62. >
  63. >ANdy
  64. >
  65. >
  66. >
  67. >
  68.  
  69.